#pragma warning(disable:4996)
#include<iostream>
#include<string>
using namespace std;

int n = 0;//字符数
int word = 0;//单词数
int sentence = 0;//句子数
int line = 1;//行数
int bline = 0;//空行
int ann = 0;//注释
int main(int argc, char* argv[])
{
	FILE* fp = fopen(argv[2], "r");
	if (fp == NULL)//判断是否打开文件
	{
		cout << "打开失败" << n << endl;
		return 0;
	}
	else if (strcmp(argv[1], "-c") == 0)//输入'-c'统计除空格外字符数
	{
		char i;
		while (!feof(fp))//文件读取到结尾时停止
		{
			i = fgetc(fp);//读取一个字符
			if (i != ' ' && i != '\n' && i != '\t')//非空格，换行符时字符数+1
			{
				n++;
			}
		}
		cout << "字符数：" << n << endl;
	}
	else if (strcmp(argv[1], "-w") == 0)//输入'-w'统计单词数
	{
		char m;
		while (!feof(fp))
		{
			m = fgetc(fp);
			if (m == ' ' || m == ',' || m == '.' || m == '!' || m == '?' || m == '\n')//读取到空格，逗号等单词分隔字符时单词数+1
			{
				word++;
			}
		}
		cout << "单词数为：" << word << endl;
	}
	else if (strcmp(argv[1], "-s") == 0)//输入'-s'统计句子数
	{
		char s;
		while (!feof(fp))
		{
			s = fgetc(fp);
			if (s == '.' || s == '?' || s == '!')//读取到句号、问号、感叹号等句子结尾标识符时句子数+1
			{
				sentence++;
			}
		}
		cout << "句子数：" << sentence << endl;
	}
	else if (strcmp(argv[1], "-l") == 0)//输入'-l'统计行数
	{
		char a;
		while (!feof(fp))
		{
			a = fgetc(fp);
			if (a == '\n')//读取到换行时行数+1,最后一行无换行符，故初始值设为1
			{
				line++;
			}
		}
		cout << "行数：" << line << endl;
	}
	else if (strcmp(argv[1], "-b") == 0)//输入'-b'统计空行数
	{
		char b;
		int num = 0;
		while (!feof(fp))
		{
			b = fgetc(fp);
			if (b == '\n')//一行内不超过一个字符判断为空行
			{
				if (num <= 1)
				{
					bline++;
				}
				num = 0;

			}
			else if (b != ' ' && b != '\t')
				num++;
		}
		cout << "空行数：" << bline << endl;
	}
	else if (strcmp(argv[1], "-a") == 0)//输入'-a'统计注释行数
	{
		char c;
		int nume = 0;
		while (!feof(fp))
		{
			c = fgetc(fp);
			if (c == '/')//检测'/'数，统计'/---/'与'//'两种注释方式
			{
				nume++;
			}
			else if (c == '\n' && nume == 1)//奇数个'/'时，在下一个'/'出现前，后面部分均为注释行
			{
				ann++;
			}
			else if (c == '\n' && nume == 2)//偶数个时，'//'注释为一注释行，'/---/'注释统计结尾行
			{
				ann++;
				nume = 0;
			}
		}
		cout << "注释行数：" << ann << endl;
	}
	fclose(fp);
}